import * as React from 'react';
import { Subscription } from 'rxjs';
import { Redirect } from 'react-router-dom';

import authService from '../../../services/svc.auth';
import { RealLoginPage } from './real-login-page';

export default class LoginPage extends React.Component {
  private subjects: Subscription | undefined;

  state = {
    authenticated: false,
  };

  componentDidMount() {
    this.subjects = authService.user.subscribe(({ authenticated }) => {
      this.setState({ authenticated });
    });
  }

  componentWillUnmount() {
    this.subjects.unsubscribe();
  }

  render() {
    if (this.state.authenticated) {
      return <Redirect to="/" />;
    } else {
      return <RealLoginPage />;
    }
  }
}
